package com.shortlink.auth.config;

import cn.hutool.core.text.AntPathMatcher;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import java.util.Set;

/**
 * 权限配置属性
 *
 * @author wp
 */
@Data
@ConfigurationProperties(prefix = "shortlink-auth.auth")
@Component
public class AuthProperties {
    /**
     * 登录权限
     */
    private Set<String> login;
    /**
     * 内部权限
     */
    private Set<String> inter;
    /**
     * 路径匹配规则
     */
    private final AntPathMatcher antPathMatcher = new AntPathMatcher();

    public boolean isRequireLogin(String path) {
        if (login.isEmpty()) {
            return false;
        }
        for (String loginPath : login) {
            if (antPathMatcher.match(loginPath, path)) {
                return true;
            }
        }
        return false;
    }

    public boolean isRequireInter(String path) {
        if (inter.isEmpty()) {
            return false;
        }
        for (String interPath : inter) {
            if (antPathMatcher.match(interPath, path)) {
                return true;
            }
        }
        return false;
    }
}
